package com.ruoyi.web.controller.shop.datashow;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.web.controller.shop.datashow.excel.DataShowExcel;
import com.ruoyi.web.controller.shop.shop.domain.ShopShop;
import com.ruoyi.web.controller.shop.shop.service.IShopShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 功能：
 *
 * @author：F_baisi
 * @create：2020/11/10 11:15
 * @version：2020 Version：1.0
 * @company：小牛科技 Created with IntelliJ IDEA
 */
@RestController
@RequestMapping("/datashow")
public class DataShowController{

	@Autowired
	private IShopShopService shopShopService;

	@Autowired
	private TokenService tokenService;

	/**
	 *
	 * @param dayList 日期数组 2020-11-15
	 * @param type 0 销量排序 1 金额排序
	 * @return
	 */
	@GetMapping
	public AjaxResult shopDataList(@RequestParam List<String> dayList,@RequestParam Integer type){
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		SysUser user = loginUser.getUser();
		//查询店铺信息
		ShopShop shopShop = this.shopShopService.selectShopShopById(user.getShopId());
		if(shopShop==null){
			return AjaxResult.error("门店不存在");
		}
		ShopShop select = new ShopShop();
		select.setParentId(shopShop.getId());
		select.setCheck(1);
		select.setStatus(1);
		//查询店铺列表
		List<ShopShop> shopShops = this.shopShopService.selectShopShopList(select);
		if(type==0){
			Collections.sort(shopShops, new Comparator<ShopShop>() {
				@Override
				public int compare(ShopShop o1, ShopShop o2) {
					if(o1.getByDaySole()<o2.getByDaySole()){
						return 1;
					}
					if(o1.getByDaySole()>o2.getByDaySole()){
						return -1;
					}
					return 0;
				}
			});
		}else{
			Collections.sort(shopShops, new Comparator<ShopShop>() {
				@Override
				public int compare(ShopShop o1, ShopShop o2) {
					if(o1.getByDayMoeny().compareTo(o2.getByDayMoeny())<0){
						return 1;
					}
					if(o1.getByDayMoeny().compareTo(o2.getByDayMoeny())>0){
						return -1;
					}
					return 0;
				}
			});
		}
		return AjaxResult.success(shopShops);
	}


	/**
	 * 数据展示
	 * @param name
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@GetMapping("/count2")
	public AjaxResult count2(@RequestParam(required = false) String name,
							 @RequestParam(required = false) String startTime,
							 @RequestParam(required = false) String endTime){
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		SysUser user = loginUser.getUser();
		//查询店铺信息
		ShopShop shopShop = this.shopShopService.selectShopShopById(user.getShopId());
		if(shopShop==null){
			return AjaxResult.error("门店不存在");
		}
		ShopShop select = new ShopShop();
		select.setParentId(shopShop.getId());
		select.setCheck(1);
		select.setStatus(1);
		select.setName(name);
		//查询店铺列表
		List<ShopShop> shopShops = this.shopShopService.selectShopShopList(select);
		return AjaxResult.success(shopShops);
	}


	/**
	 * 数据展示count2导出
	 * @param name
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	@GetMapping("/count2/excel")
	public AjaxResult count2Excel(@RequestParam(required = false) String name,
								  @RequestParam(required = false) String startTime,
								  @RequestParam(required = false) String endTime){
		LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
		SysUser user = loginUser.getUser();
		//查询店铺信息
		ShopShop shopShop = this.shopShopService.selectShopShopById(user.getShopId());
		if(shopShop==null){
			return AjaxResult.error("门店不存在");
		}
		ShopShop select = new ShopShop();
		select.setParentId(shopShop.getId());
		select.setCheck(1);
		select.setStatus(1);
		select.setName(name);
		//查询店铺列表
		List<ShopShop> shopShops = this.shopShopService.selectShopShopList(select);
		//赋值准备导出的类
		ArrayList<DataShowExcel> dataShowExcels = new ArrayList<DataShowExcel>();
		for(ShopShop shop:shopShops){
			DataShowExcel dataShowExcel = new DataShowExcel();
			dataShowExcel.setAllMoney(shop.getAllMoney());
			dataShowExcel.setGradeEnvironment(shop.getGradeEnvironment());
			dataShowExcel.setGradeQuality(shop.getGradeQuality());
			dataShowExcel.setGradeService(shop.getGradeService());
			dataShowExcel.setName(shop.getName());
			dataShowExcel.setNote(shop.getNote());
			dataShowExcels.add(dataShowExcel);
		}
		ExcelUtil<DataShowExcel> util = new ExcelUtil<DataShowExcel>(DataShowExcel.class);
		return util.exportExcel(dataShowExcels, "datashow");
	}

}
